package com.slofzx.nuoda.evaluation.project.compont.imported;

import com.slofzx.nuoda.common.exception.ServiceException;
import com.slofzx.nuoda.evaluation.project.enums.ProductTypeEnum;
import com.slofzx.nuoda.evaluation.project.compont.imported.AbstractImportExcelComponent;
import com.slofzx.nuoda.evaluation.project.dao.PlanInfoDAO;
import com.slofzx.nuoda.evaluation.project.entity.PlanInfo;
import com.slofzx.nuoda.evaluation.project.enums.EvaluationMethodEnum;
import com.slofzx.nuoda.evaluation.project.util.BusiUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Component
public class PlanImportExcelComponent extends AbstractImportExcelComponent {
    @Autowired
    private PlanInfoDAO planInfoDAO;


    public String getTemplatePath(ProductTypeEnum productType) {
        if (ProductTypeEnum.GAS == productType) {
            return GAS_PLAN_TEMPLATE;
        }
       return PLAN_TEMPLATE;
    }

    @Override
    public void updateCostEvaMethod(String targetId, String evaMethod) {
        final PlanInfo planInfo = planInfoDAO.selectById(targetId);
        planInfo.setCostEvaMethod(evaMethod);
        planInfoDAO.updateById(planInfo);
    }

    @Override
    public String getMapType(Map<String, String> fieldPositionMap, Map<String, String> rowData) {

        String projectPosition = fieldPositionMap.get("evaluationMethod");
        String typeColIndex = projectPosition.split(BusiUtil.DELIMITER)[1];
        String type = rowData.get(typeColIndex);
        final EvaluationMethodEnum enumByName = BusiUtil.getEnumByName(type, EvaluationMethodEnum.class);
        if (enumByName != null) {
            type = enumByName.getId();
        }
        return type;

    }

    @Override
    public void checkProject(Map<String, String> fieldPositionMap, List<Map<String, String>> dataList, Integer startRowIndex) {
        if ((dataList.size() - startRowIndex) > 3) {
            throw new ServiceException("任务性质为方案的任务项目不允许超过3个", "任务性质为方案的任务项目不允许超过3个", null);
        }
        final String evaluationMethodPosition = fieldPositionMap.get("evaluationMethod");
        String[] split = evaluationMethodPosition.split(BusiUtil.DELIMITER);
        String colStr = split[1];
        Set<String> evaluationMethodSet = new HashSet<>();
        for (int i = startRowIndex; i < dataList.size(); i++) {
            final Map<String, String> rowData = dataList.get(i);
            String evaluationMethod = rowData.get(colStr);
            if (EvaluationMethodEnum.GROUND.getName().equals(evaluationMethod)) {
                throw new ServiceException("任务性质为方案的项目，评价方法不允许为地面评价", "任务性质为方案的项目，评价方法不允许为地面评价", null);
            }
            // 添加不进去说明，当前元素为重复的
            if (!evaluationMethodSet.add(evaluationMethod)) {
                throw new ServiceException("任务性质为方案的项目，评价方法不能重复，重复评价方法名：" + evaluationMethod, "任务性质为方案的项目，评价方法不能重复，重复评价方法名：" + evaluationMethod, null);
            }
        }
        evaluationMethodSet.clear();

    }
}
